package com.michael.study.arouter;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.michael.study.arouter.databinding.ActivityMainBinding;
import com.michael.study.arouter.service.HelloService;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ActivityMainBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        mBinding.btnActivity.setOnClickListener(this);
        mBinding.btnFragment.setOnClickListener(this);
        mBinding.btnModule.setOnClickListener(this);
        mBinding.btnServiceByName.setOnClickListener(this);
        mBinding.btnServiceByType.setOnClickListener(this);
        mBinding.btnFail1.setOnClickListener(this);
        mBinding.btnFail2.setOnClickListener(this);
        mBinding.btnFail3.setOnClickListener(this);

        // AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        testRxJava();
    }

    private void testRxJava() {
        ObservableOnSubscribe subscribe =
                new ObservableOnSubscribe() {
                    @Override
                    public void subscribe(ObservableEmitter emitter) throws Exception {
                        Log.d("lgq", "subscribe: "+Thread.currentThread().getName());
                        emitter.onNext("1234");
                        emitter.onComplete();
                    }
                };
        Observable.create(subscribe)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {}

                            @Override
                            public void onNext(String o) {
                                Log.d("lgq", "onNext: a" + o);
                                Log.d("lgq", "onNext: " + Thread.currentThread().getName());
                            }

                            @Override
                            public void onError(Throwable e) {}

                            @Override
                            public void onComplete() {}
                        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_activity) {
            ARouter.getInstance()
                    .build(RoutePath.TEST_ACTIVITY_1)
                    .withString("name", "liuguoquan")
                    .navigation(
                            this,
                            100,
                            new NavCallback() {
                                @Override
                                public void onArrival(Postcard postcard) {
                                    Log.d("lgq", "onArrival: ");
                                }

                                @Override
                                public void onInterrupt(Postcard postcard) {
                                    Log.d("lgq", "onInterrupt: 被拦截了");
                                }
                            });
        }
        if (v.getId() == R.id.btn_fragment) {
            Fragment fragment =
                    (Fragment)
                            ARouter.getInstance()
                                    .build("/test/fragment")
                                    .withCharSequence("name", "liuguoquan")
                                    .navigation();
            if (fragment != null) {
                Log.d("lgq", "onClick: " + fragment.getClass().getSuperclass().getName());
            } else {
                Log.d("lgq", "onClick: fragment == null");
            }
        }
        if (v.getId() == R.id.btn_module) {
            ARouter.getInstance().build(RoutePath.TEST_MODULE_1).navigation();
        }

        if (v.getId() == R.id.btn_service_by_name) {
            HelloService service =
                    (HelloService)
                            ARouter.getInstance().build(RoutePath.TEST_SERVICE_HELLO).navigation();
            service.sayHello("zxy");
        }

        if (v.getId() == R.id.btn_service_by_type) {
            ARouter.getInstance().navigation(HelloService.class).sayHello("liuguoquan");
        }

        if (v.getId() == R.id.btn_fail1) {
            ARouter.getInstance()
                    .build("/xxx/xxx")
                    .navigation(
                            this,
                            new NavCallback() {
                                @Override
                                public void onFound(Postcard postcard) {
                                    Log.d("lgq", "找到了");
                                }

                                @Override
                                public void onLost(Postcard postcard) {
                                    Log.d("lgq", "找不到了");
                                }

                                @Override
                                public void onArrival(Postcard postcard) {
                                    Log.d("lgq", "跳转完了");
                                }

                                @Override
                                public void onInterrupt(Postcard postcard) {
                                    Log.d("lgq", "被拦截了");
                                }
                            });
        }

        if (v.getId() == R.id.btn_fail2) {
            ARouter.getInstance().build("/xxx/xxx").navigation();
        }

        if (v.getId() == R.id.btn_fail3) {
            ARouter.getInstance().navigation(MainActivity.class);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            if (resultCode == Activity.RESULT_OK) {
                Log.d("lgq", "onActivityResult: ");
            }
        }
    }

    private void getMemo(){
        //获得ActivityManager服务的对象
        ActivityManager mActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
        //获得MemoryInfo对象
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo() ;
        //获得系统可用内存，保存在MemoryInfo对象上
        mActivityManager.getMemoryInfo(memoryInfo) ;
        long memSize = memoryInfo.availMem ;
        Log.d("lgq", "getMemo: " + memoryInfo.availMem / 1024 / 1024 + " ");
        Log.d("lgq", "getMemo: " + memoryInfo.totalMem / 1024 / 1024 + " ");

    }
}
